package cn.silver.module.monitor.bean;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serial;
import java.io.Serializable;
import java.util.Properties;

/**
 * @author zhouxiaobo
 */
@Data
@NoArgsConstructor
@Schema(name = "RedisBean", title = "redis基本信息对象")
public class RedisBean implements Serializable {
    @Serial
    private static final long serialVersionUID = 1L;
    @Schema(title = "服务器地址")
    private String ipaddr;
    @Schema(title = "访问端口")
    private Integer port;
    @Schema(title = "服务器版本")
    private String version;
    @Schema(title = "部署模式")
    private String mode;
    @Schema(title = "CPU占用率")
    private String cpu;
    @Schema(title = "最大内存")
    private String maxMemory;
    @Schema(title = "已用内存")
    private String useMemory;
    @Schema(title = "运行时常")
    private Long runtime;
    @Schema(title = "key的总数")
    private Long dbSize;
    @Schema(title = "客户端连接数")
    private Integer clients;
    @Schema(title = "持久化状态")
    private Boolean rdb;
    @Schema(title = "AOF是否开启")
    private Boolean aof;
    private String input;
    private String output;

    public void reload(Properties info) {
        this.port = Integer.valueOf(info.getProperty("tcp_port"));
        this.version = info.getProperty("redis_version");
        this.mode = info.getProperty("redis_mode");
        this.cpu = info.getProperty("used_cpu_sys");
        this.maxMemory = info.getProperty("maxmemory_human");
        this.useMemory = info.getProperty("used_memory_human");
        this.runtime = Long.valueOf(info.getProperty("uptime_in_seconds"));
        this.clients = Integer.valueOf(info.getProperty("connected_clients"));
        this.rdb = "ok".equals(info.getProperty("rdb_last_bgsave_status"));
        this.aof = "0".equals(info.getProperty("aof_enabled"));
        this.input = info.getProperty("instantaneous_input_kbps");
        this.output = info.getProperty("instantaneous_output_kbps");
    }
}
